package com.ifnk.portaljava.dapWeb.socket;
/**
 * <p>Title: 处理RawData数据包类</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author WenSheng.Song.hlyt@gmail.com
 * @version 1.0
 */

import com.ifnk.portaljava.dapWeb.dapweb.CDRPacket;
import com.ifnk.portaljava.dapWeb.dapweb.Command;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

public class ProcessRawData extends Thread {
	protected Log loger = LogFactory.getLog(ProcessRawData.class);
	protected int count = 0;
	protected boolean flag = true;
	protected ClientProperty cp;
	protected byte[] packet;
	protected Command command = new Command();
	protected CDRPacket cdrPacket = new CDRPacket();


	public ProcessRawData(ClientProperty cp) {
		this.cp = cp;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public void run() {
		while (flag) {
			if (cp.getRawDataPackets().isEmpty()) {
				count++;
				try {
					if (count > 3) {
						cp.doRawDataThreads.remove(currentThread());
						cp.processRawData = null;
						flag = false;
						break;
					} else {
						Thread.sleep(100);
						continue;
					}
				} catch (Exception e) {
					e.printStackTrace();
					cp.doRawDataThreads.remove(currentThread());
					cp.processRawData = null;
					loger.error("WebServer Exception " + e.toString());
					break;
				}
			} else {
				packet = cp.getRawDataPackets().poll();
				processPkt(packet); // 这个方法在 ProcessRawDataAll.java   47行 的 processPkt(packet);
			}
		}
		cp.doRawDataThreads.remove(currentThread());
		cp.processRawData = null;
	}

	public void processPkt(byte[] packet) {
		
/*		try {
			command.DecodeRawDataII(packet);
			if (command.getCdrEndFlag() == 1) {
//				flag = false;
				loger.info("WebServer receive the CDR's CdrEndFlag=1");
			}
			if (cp.getRequestDateFromProxyId().equals(command.getSessionId())) {
//				loger.info("WebServer Receive WWW_RAWDATA "	+ command.getSequenceNo());
				@SuppressWarnings("unused")
				CDRBean cdrBean = null;

				String key = command.getSvrId() + "_" + command.getCdrId() + "_" + command.getChar_protocol()+ "_" + command.getTs_s();
				
//				loger.info("WebServer received CDR's key = " + key);
				if (cp.cdrPktInfoMap.containsKey(key)) {
					@SuppressWarnings("unused")
					String subTopic = "message" + cp.getClientId();
					cdrBean = (command.getPkt().length == 0) ? new CDRBean() : cdrPacket.processCDRPacket(command, cp);//Tshark
				} else {
					loger.error("WebServer key not current client key : " + key);
				}
			} else {
				loger.error("WebServer sessionId not current client sessionId");
			}
		} catch (Exception e) {
			loger.error("WebServer Exception " + e.toString());
			loger.error("WebServer Exception CdrEndFlag: "
					+ command.getCdrEndFlag());
			e.printStackTrace();
		}*/
	}
	
	public void deleteSessionFiles() {
		
	}
}
